{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1596269082211",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'Alex': 2020010483,\n 'Jack': 2019880789,\n 'Tim': [2015050888, 'Phd'],\n 'Eva': [2016050999, 'Master']}"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "# dict\n",
    "names = {\n",
    "    \"Alex\": 2020010483,\n",
    "    \"Jack\": 2019880789,\n",
    "    \"Tim\": [2015050888, \"Phd\"],\n",
    "    \"Eva\": [2016050999, \"Master\"],\n",
    "}\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 三步法\n",
    "f = open(r\"Data_samples\\names.json\", \"w\")\n",
    "# f.write(names)\n",
    "json.dump(names, f)  # 把字典对象names写入文件流f\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以上三步法可以简写为\n",
    "with open(r\"Data_samples\\names.json\",\"w\") as f:\n",
    "    json.dump(names,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从 json 文件 \"JSON_sample.json\" 读取数据，存入变量 data\n",
    "with open(r\"Data_samples\\JSON_sample.json\",\"rb\") as f:\n",
    "    data = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "<class 'dict'>\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'msg': '',\n 'data': {'count': 241,\n  'previous': None,\n  'results': [{'to_user': 14442848,\n    'liked': 0,\n    'user_id': 31084077,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 31084077,\n     'name': '',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://pbp38mcp7.bkt.clouddn.com/15537581968204.png',\n     'nickname': ''},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'ids': [29487489,\n     10708417,\n     30890058,\n     4176075,\n     31084077,\n     3290321,\n     28915029,\n     29116055,\n     13263288,\n     30884606],\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1596075422000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13724296},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 28915029,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 28915029,\n     'name': '王婷婷',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://thirdwx.qlogo.cn/mmopen/aDRKsRebBibdoZ8B4vMdBevIl4zJ3GMScOt9tfcFgfK5NC52icObNG0cAxGDianhqvcibiaUjUDgBZgmNgugFNTKKQsm8acYZPAqM/132',\n     'nickname': '婷'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1596025152000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13716365},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 3290321,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 3290321,\n     'name': '王瑀聖',\n     'school_number': '0134',\n     'role': 5,\n     'avatar': 'https://qn-sx.yuketang.cn/avatar/e5V3I4O8Iejdb5dTlNcUJpVTEicPdSnicOnu7EWxZpZD0KkpdaC7MKVhiavaD7kbzsiciaUIB4C588EE9BkTKNzNqsglib7GSXoc0j',\n     'nickname': '王瑀聖'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1595921616000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13693681},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 13263288,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 13263288,\n     'name': 'LANG',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://thirdwx.qlogo.cn/mmopen/GibOcMnJ0cVLUBTHWuJYeBjWL38v53oGde9Dr2iaicbkSqmeA3ibA7GOzDt9bQlt2T4oNjmGFtjDOr9gcOBcJasOad5lctZgHZpg/132',\n     'nickname': 'LANG'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1595899800000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13687233},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 29487489,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 29487489,\n     'name': '',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://thirdwx.qlogo.cn/mmopen/K3V79ej69wM6Hibia7pQF8TfrHkPIKY0zTNiaqYWPMMAHIEvJZolDPQKNictxnPzHwJcPkicZJ5A5QxWNebice8icLiasjUblickHxo1W/132',\n     'nickname': '小允'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1595828623000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13674747},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 30884606,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 30884606,\n     'name': '',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://thirdwx.qlogo.cn/mmopen/vi_32/anV2LaMkvia8icvfml5OduCSKbhJGttKhBukKH8JTyMRPxvcj4WZl5X9zqdDUn9sAy49IepKZCCU5aLVp2UYgWJw/132',\n     'nickname': '清'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1595581794000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13628893},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 4176075,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 4176075,\n     'name': '徐焕韬',\n     'school_number': '201511222033',\n     'role': 5,\n     'avatar': 'https://qn-sx.yuketang.cn/avatar/QLR5aKwLYaMobVKTzZapalCZNAibW0NMy6YGqKibQqYfdN7Ef16dOrpHqIs1mX4MlnkPBTk3VtC8FIS9WicicKRf5DnJU9Ilg07T',\n     'nickname': 'Rank'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1595559279000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13623012},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 29116055,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 29116055,\n     'name': '',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://pbp38mcp7.bkt.clouddn.com/15537581968204.png',\n     'nickname': ''},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1595332275000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13581833},\n   {'to_user': 10708417,\n    'liked': 0,\n    'user_id': 10708417,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 10708417,\n     'name': '胡东泽',\n     'school_number': '201905710907',\n     'role': 5,\n     'avatar': 'https://qn-sx.yuketang.cn/avatar/QLR5aKwLYaM84WhVs7re5FBrcMJn9tvC3PTHsBhwhDVrmuZg88dn72WCQb5neWiaZZlswwiamc7HgpdX9tkhyIcu8dLjXC3KNK',\n     'nickname': '弎水'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1594184151000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13310398},\n   {'to_user': 14442848,\n    'liked': 0,\n    'user_id': 30890058,\n    'is_essence': 0,\n    'replys': [],\n    'deleted': False,\n    'user_info': {'user_id': 30890058,\n     'name': '',\n     'school_number': '',\n     'role': 5,\n     'avatar': 'http://thirdwx.qlogo.cn/mmopen/nYYvURlqaQbHnbBmOZbUmnzNjcftYkORDG7icKaNnq9vKb7aqZV3VrF5zUNdXYVjGO1vbexcAibxN2ufowFJBp5jeKNdic941HS/132',\n     'nickname': '木易aka'},\n    'topic': 899016,\n    'classroom_id': 3966931,\n    'commented': 0,\n    'content': {'app_upload_images': [],\n     'text': '打卡',\n     'app_text': '打卡',\n     'upload_images': []},\n    'is_delete': 0,\n    'is_top': 0,\n    'create_time': 1594105187000,\n    'score_status': 0,\n    'is_self': 0,\n    'is_praise': 0,\n    'score': 0,\n    'id': 13272889}],\n  'next': 'http://internal-xuetang-internal-1320283413.cn-north-1.elb.amazonaws.com.cn/forum/api/forum/v2/comments/?appid=NEW_MASTER_STATION&channel=xuetangx_student&is_essence=True&limit=10&offset=10&topic_id=899016&user_id=71853'},\n 'success': True}"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "print(type(data))\n",
    "data[\"0\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'to_user': 14442848,\n 'liked': 0,\n 'user_id': 31084077,\n 'is_essence': 0,\n 'replys': [],\n 'deleted': False,\n 'user_info': {'user_id': 31084077,\n  'name': '',\n  'school_number': '',\n  'role': 5,\n  'avatar': 'http://pbp38mcp7.bkt.clouddn.com/15537581968204.png',\n  'nickname': ''},\n 'topic': 899016,\n 'classroom_id': 3966931,\n 'ids': [29487489,\n  10708417,\n  30890058,\n  4176075,\n  31084077,\n  3290321,\n  28915029,\n  29116055,\n  13263288,\n  30884606],\n 'commented': 0,\n 'content': {'app_upload_images': [],\n  'text': '打卡',\n  'app_text': '打卡',\n  'upload_images': []},\n 'is_delete': 0,\n 'is_top': 0,\n 'create_time': 1596075422000,\n 'score_status': 0,\n 'is_self': 0,\n 'is_praise': 0,\n 'score': 0,\n 'id': 13724296}"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "data[\"0\"]['data']['results'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}